package 原型模式.深克隆;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author wzx
 * @date 2024/9/5 16:54
 * @description: 测试
 */
public class TestPrototype {

    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {

        ConcretePrototype c1 = new ConcretePrototype();
        Person p1 = new Person("张三");
        c1.setPerson(p1);
        // 创建对象序列化输出流
        ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(Paths.get("a.txt")));
        // 将c1对象写到文件
        oos.writeObject(c1);
        oos.close();

        // 创建对象序列化输入流
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("a.txt"));
        // 读取对象
        ConcretePrototype c2 = (ConcretePrototype)ois.readObject();
        Person p2 = c2.getPerson();
        p2.setName("李四");

        System.out.println("对象c1和c2是同一个对象吗？："+(c1 == c2));

        c1.show();
        c2.show();
        System.out.println("对象c1和c2是同一个对象吗？："+(c1 == c2));
    }



}
